/*
 * MenuAction.java
 * 
 * Version 1.0
 *
 * 13-08-2008
 * 
 * Copyright notice
 * 
 * Modification Logs:
 *   DATE        AUTHOR      DESCRIPTION
 *  --------------------------------------------------------
 *  13-Aug-2008  Thuydtn1     Tao MenuAction Class
 *  18-Aug-2008	 Thuydtn1	  Perform method execute
 */

package vn.fpt.fsoft.fcos.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vn.fpt.fsoft.fcos.entity.UserEntity;
import vn.fpt.fsoft.fcos.form.MenuForm;
import vn.fpt.fsoft.fcos.util.Constant;


/**
 * The MenuAction Class.
 * @author DaoThuy
 *
 */
public class MenuAction extends Action {
	
/**
 * This method check exising of User instance in the session and display menu.
 * 
 * @param mapping ActionMaping: The ActionMapping used to select this Login instance																										
 * @param form ActionForm: The LoginForm bean for request
 * @param request HttpServletRequest: The HTTP request that Login is processing 
 * @param response HttpServletResponse: The HTTP response that Login is creating
 * 
 * @throws Exception if the menu logic throws an exception.
 * 
 * @return forward names to correlative menu items: MENU_000, MENU_001, MENU_002, MENU_003.
 */
	@Override
	public ActionForward execute(ActionMapping mapping, 
									ActionForm form,
									HttpServletRequest request, 
									HttpServletResponse response)
									throws Exception {
		
		// Get UserEntity in session with key "SK_USER_ENTITY"
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session.getAttribute(Constant.SK_USER_ENTITY);
		// Check UserEntity is existed or not?
		// If existed, go to step 	
		if (userEntity != null) {
			// Checking value of processId in menuForm
			MenuForm menuForm = (MenuForm) form;
			String processId = menuForm.getProcessID();
			// If it is empty, getting the correlative ActionForward of "init".
			if ((processId == null) && (processId.isEmpty())) {
				return mapping.findForward(Constant.INIT);
			}
			// If the processId is MENU_1xx, then check the authorization of login user.
			// If role of Entity User is not Manager:
			// Processing error with message id "M102". Stop the proces.
			else if (processId.indexOf(Constant.MENU_1) != -1) {
				// Getting the correlative ActionForward of processId.
				session.setAttribute(Constant.SK_ERRORCODE, Constant.MENU_102);
			}
			// If the processId is MENU_0xx, then check the authorization of login user.
			// Available of forward name from the menu
			else if (processId.equals(Constant.MENU_000) || 
					processId.equals(Constant.MENU_001) || 
					processId.equals(Constant.MENU_002) || 
					processId.equals(Constant.MENU_103)) {
				return mapping.findForward(processId);
			}
		}
		// Otherwise, add into the session with a pair data (key, value):
		else {
			session.setAttribute(Constant.SK_ERRORCODE, Constant.MENU_101);
		}
		return mapping.findForward("init");
	}
}
